<?php
html::printPath('person.form-title');
$msg->printMessages();
?>

<ul class="commandList">
    <li class="addLi"><?php echo html::anchor('admin/person/edit', text::get('person.createNew')) ?></li>
    <li class="searchCommand"><?php echo html::searchEntityAnchor($this); ?></li>
</ul>

<?php
echo '<div class="quickSearchField">';
$fb = new FormBuilder('person', $entity);
$fb->setAction('/admin/person/quickSearch');
$fb->autocompleter('quickSearch_id', $personOptions)->mustMatch()->labelKey('application.quickSearch');
$fb->write();
echo '</div>';

echo '<ul class="commandList">';
echo '<li class="exportToExcel">' . html::anchor('/admin/person/exportToExcel', text::get('application.exportToExcel')) . '</li>';
echo '</ul>';

$elb = new EntityListBuilder($this, $personList);
$elb->text('last_name');
$elb->text('first_name');
$elb->date('birth_date');
$elb->date('email');
$elb->text('country_name')->labelKey('country_code');
$elb->text('login');
$elb->boolean('is_admin');
$elb->boolean('is_blocked');
$elb->write();
?>

<!--
<button id="columnSelector">Choose columns</button>
<table id="list"></table>
<div id="pager"></div>
-->

<script type="text/javascript">
jQuery(document).ready(function() {
    
    return;

    var gridHeight = $(document).height() - 320;
    var colModel = null;
    var width = null;
    var shrinkToFit = false;
    <?php
    /*
    if ($_SESSION['colModel']) {
        echo 'colModel = ' . $_SESSION['colModel'] . ";\n";
    }
    if ($_SESSION['width']) {
        echo 'width = ' . $_SESSION['width'] . ";\n";
        echo "shrinkToFit = false;\n";
    }*/
    ?>;

    if (! colModel) {
        colModel = [
          {name:'last_name', index:'last_name', width:90},
          {name:'first_name', index:'first_name', width:90},
          {name:'birth_date', index:'birth_date', width:140, align:'right'},
          {name:'country_code', index:'country_name', width:150, align:'right'},
          {name:'login', index:'login', width: 160, align:'right'},
          {name:'is_admin', index:'is_admin', width:180, sortable:false},
          {name:'is_blocked', index:'is_blocked', width:150, sortable:false}
        ];
    }

    jQuery("#list").jqGrid({
        url: '<?php echo url::site('/admin/person/allByAjax') ?>',
        datatype: 'json',
        mtype: 'GET',
        colNames:['Příjmení','Jméno', 'Datum narození','Stát','Uživatelské jméno','Je správcem systému','Účet je blokován'],
        colModel :colModel,
        pager: '#pager',
        sortname: 'invid',
        sortorder: 'desc',
        viewrecords: true,
        height: gridHeight,
        width: width,
        shrinkToFit: shrinkToFit,
        resizeStop: function(newwidth, index) {
            adaptGridWidth();
            saveGridLayout();
        }
    });

    adaptGridWidth();

    function gridWidth() {
        var dataPanel = $('#gbox_list div.ui-jqgrid-bdiv')[0];
        var headerBox = $('#gview_list .ui-jqgrid-hbox');
        var gridWidth;
        if (dataPanel.scrollWidth > dataPanel.offsetWidth) {
            gridWidth = dataPanel.scrollWidth;
        }
        else {
            gridWidth = headerBox.width();
        }
        return gridWidth + parseInt(headerBox.css('padding-right'));
    }

    function adaptGridWidth() {
        $('#list').setGridWidth(gridWidth(), false);
    }

    function saveGridLayout() {
        var layout = {
            width: gridWidth(),
            colModel: jQuery.toJSON($('#list').getGridParam('colModel'))
        }
        $('#layoutString').html(layout.colModel + "<br/>" + layout.width);
        jQuery.post('saveLayout.php', layout);
    }

    jQuery("#columnSelector").click(function() {
        // http://www.trirand.com/jqgridwiki/doku.php?id=wiki:jquery_ui_methods#column_chooser
        jQuery("#list").columnChooser({
            done: function(perm) {
                if (perm) {
                    jQuery("#list").remapColumns(perm, true);
                }
                adaptGridWidth();
                saveGridLayout();
                return true;
            }
        });
        return false;
    });

    jQuery("#gbox_list").resizable({
        handles: 'e',
        stop: function(event, ui) {
            $('#list').setGridWidth(jQuery("#gbox_list").width(), false);
        }
    });
});
</script>